package com.boarsoft.message.spy;

import org.springframework.util.Assert;

import com.boarsoft.message.MessageConfig;
import com.boarsoft.message.bean.Message;
import com.boarsoft.soagov.svc.SvcReqReader;

/**
 * 用于从Message对象获取必要的服务治理信息
 * 
 * @author Mac_J
 *
 */
public class MsgReqReaderImpl implements SvcReqReader {

	@Override
	public Object pick(Object input, String key) {
		Assert.notNull(input, "Input can not be null");
		Message m = (Message) input;
		// 只支持以下字段
		switch (key) {
		case "acknowledge":
			return m.getAcknowledge();
		case "attachement":
			return m.getAttachment();
		case "content":
			return m.getContent();
		case "from":
			return m.getFrom();
		case "fromAddr":
			return m.getFromAddr();
		case "id":
			return m.getId();
		case "needAcknowledge":
			return m.isNeedAcknowledge();
		case "relay":
			return m.isRelay();
		case "key":
			return m.getKey();
		case "recipients":
			return m.getRecipients();
		case "lastTrace":
			return m.lastTrace();
		case "status":
			return m.getStatus();
		case "tag":
			return m.getTag();
		case "target":
			return m.getTarget();
		case "time":
			return m.getTime();
		case "title":
			return m.getTitle();
		case "traces":
			return m.getTraces();
		case "tried":
			return m.getTried();
		default:
			throw new UnsupportedOperationException();
		}
	}

	@Override
	public String getCode(Object input) {
		Assert.notNull(input, "Input can not be null");
		Message m = (Message) input;
		String a = m.getFromAddr();
		a = a.equals(MessageConfig.getAddr()) ? "/producer" : "/consumer";
		return new StringBuilder(m.getTarget()).append(a)//
				.append("/com.boarsoft.soagov.spy.SvcSpy/1.0").toString();
	}
}
